//
//  KKTabBarItem.swift
//  KKWeibo
//
//  Created by kassia on 16/7/13.
//  Copyright © 2016年 kassia. All rights reserved.
//

import UIKit

class KKTabBarItem: UITabBarItem {

   override var badgeValue: String?{
        didSet{
         //  2. 通过kvc 获取对应_target对象也就是我们的CZMainViewController
          
         let tabBarController = valueForKey("_target") as? UITabBarController
            for tabbarchild in tabBarController!.tabBar.subviews {
            
                if tabbarchild.isKindOfClass(NSClassFromString("UITabBarButton")!) {
                    for tabbarButtonChild in tabbarchild.subviews {
                    
                        if tabbarButtonChild.isKindOfClass(NSClassFromString("_UIBadgeView")!) {
                    
                            for badgeViewChild in tabbarButtonChild.subviews {
                            
                                if badgeViewChild.isKindOfClass(NSClassFromString("_UIBadgeBackground")!) {
                                    //统计这个类有多少个成员变量
                                    var count: UInt32 = 0
                                    //获取里面的成员列表
                                    let ivars = class_copyIvarList(NSClassFromString("_UIBadgeBackground")!, &count)
                                    for i in 0..<count {
                                        // 获取指定位置的成员
                                        let ivar = ivars[Int(i)]
                                        // 获取成员的名字
                                        let name = ivar_getName(ivar)
                                        let type = ivar_getTypeEncoding(ivar)
                                        // 把获取的成员名字转字符串
                                        let nameStr = String(CString: name, encoding: NSUTF8StringEncoding)
                                        
                                        let typeStr = String(CString: type, encoding: NSUTF8StringEncoding)
                                        print(nameStr)
                                        print(typeStr)
                                        
                                        //使用kvc方式给我们_image成员变量赋值
                                        if nameStr == "_image" {
                                        
                                            badgeViewChild.setValue(UIImage(named: "main_badge"), forKey: "_image")
                                        
                                        }
                                    
                                     
                                    
                                    }
                                    
                                
                                    
                                }
                                
                            
                            }
                        
                        }
                    }
                
                }
            
            }
        
        }
    
    }
    
    
}
